package com.skp.shaphan.ui.connectioncontrols;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import com.skp.shaphan.actions.SavedConnectionAction;
import com.skp.shaphan.savedConnections.SavedConnections;
import com.skp.shaphan.ui.ActionBasedPanel;
import com.skp.shaphan.ui.ConnectRunnable;
import com.skp.shaphan.ui.dialogs.ConnectionDialog;

public class SavedConnectionOptions extends JPanel implements ActionBasedPanel, ConnectionOptions {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7355143655552894994L;
	private JTable connections;
	private SavedConnectionAction action;
	private ConnectionDialog dialog;
	private JScrollPane scroll;
	
	public SavedConnectionOptions(ConnectionDialog dialog) {
		this.dialog = dialog;
		this.setLayout(new GridBagLayout());
		JLabel label;
		int row = 0;
		
		label = new JLabel("Saved Connections: ");
		this.add(label, new GridBagConstraints(0, row++, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		scroll = new JScrollPane();
		createConnectionTable();
		this.add(scroll, new GridBagConstraints(0, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
	}
	private void createConnectionTable() {
		connections = new JTable(new SavedConnectionTableModel());
		connections.setColumnSelectionAllowed(false);
		TableCellRenderer defaultRenderer = connections.getDefaultRenderer(JButton.class);
		connections.setDefaultRenderer(JButton.class, new EditButtonRenderer(defaultRenderer));
		connections.setDefaultRenderer(String.class, new EditButtonRenderer(defaultRenderer));
		connections.addMouseListener(new EditButtonMouseListener(connections));
		scroll.setViewportView(connections);
	}
	public void refreshConnectionTable() {
		createConnectionTable();
	}
	@Override
	public ConnectRunnable getAction() {
		if(action == null) {
			action = new SavedConnectionAction(dialog, this);
		}
		return action;
	}
	public int getSelectedIndex() {
		return connections.getSelectedRow();
	}
	public String getSelectedConnectionClass() {
		SavedConnections sc = SavedConnections.getInstance();
		return sc.getConnectionClass(connections.getSelectedRow());
	}
	public Properties getSelectedConnectionProperties() {
		SavedConnections sc = SavedConnections.getInstance();
		return sc.getConnectionProperties(connections.getSelectedRow());
	}
	public String getSelectedConnectionName() {
		SavedConnections sc = SavedConnections.getInstance();
		return sc.getConnectionName(connections.getSelectedRow());
	}
	@Override
	public Properties getConnectionOptions() {
		return null;
	}
	@Override
	public String getUserFriendlyName() {
		return "Saved Connections";
	}
	@Override
	public void setConnectionOptions(Properties properties) {
	}
}
